// 引入接口文件
import { parentCode, houseListGet, createConsultant, getConsultantInfo, updateConsultantInfo } from './editConsultantServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
// 初始默认状态
const state = {
  btnLoading: false,
  areaList: [],
  actionType:"",
  showAddVisible: false,
  projectList: [],
  createConsultantParams: {
    name: '',
    idCardNo: '',
    productId: '',
    province: '',
    city: '',
    sex: '',
    serialNumber: '',
    mobile: '',
    uid: '',
    displayAvgPoints:''
  },
  type: '',
    lock:false,
    isHasScore:false,
    flagZeroId:''
}
// 声明module
const tmpModule = {
  namespace: 'editConsultantModel',
  // 默认状态
  state: cloneDeep(state),
  // 入口函数(先执行)
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen( ({ pathname, query }) => {
        // 页面路由匹配时执行
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({ type: 'updateStore', payload: cloneDeep(state) });
          dispatch({ type: 'updateStore', payload:{actionType:query.type,lock:query.lock} });
          dispatch({ type: 'getHouseProjectList', payload: {} })
          if (query.type == 'edit' || query.type == 'check') {
            dispatch({ type: 'getConsultantInfo', payload: { propertyConsultantId: query.id, productId: query.productId, oldProductId: query.productId } })
          }
        }
      })
    },
  },

  // 方法列表
  effects: {
    //获取级联列表
    *getAreaList({ payload }, { put, call, select }) {
      try {
        const { data, success } = yield call(parentCode);
        if (success) {
          let areaList = []
          let arr = [data[0]]
          let attr = [data[data.length - 1]]
          data[0] = {
            label: "北京",
            value: "110100",
            children: arr
          }
          data[data.length - 1] = {
            label: "澳门",
            value: "820000",
            children: attr
          }
          //去掉第三级，
          areaList = makeAddress(data, 2,false)
          yield put({ type: 'updateStore', payload: { areaList: areaList } });
        }
      } catch (e) {

      }
    },
    //获取项目列表
    *getHouseProjectList({ payload }, { put, call, select }) {
      try {
        const { success, rows } = yield call(houseListGet, { ...payload, pageSize: 10000 })
        if (success) {
          yield put({ type: 'updateStore', payload: { projectList: rows } })
        }
      } catch (e) {
        console.log(e)
      }
    },
    // 创建置业顾问
    *createHouseConsultant({ payload }, { put, call, select }) {
      try {
        yield put({type: 'updateStore', payload: {loading: true, btnLoading: true,}})
        const { success, rows } = yield call(createConsultant, parse(payload))
        yield put({type: 'updateStore', payload: {loading: false, btnLoading: false,}})
        if (success) {
          message.success('创建成功',()=>{
            hashHistory.push({
              pathname: '/home/realEstateConsultantAdmin/consultantAdmin',
            });
          })
        }
      } catch (e) {
        console.log(e)
      }
    },
    // 更新置业顾问
    *updateConsultantInfo({ payload }, { put, call, select }) {
      try {
        console.log(payload)
        yield put({type: 'updateStore', payload: {loading: true, btnLoading: true,}})
        const { success, rows } = yield call(updateConsultantInfo, parse(payload))
        yield put({type: 'updateStore', payload: {loading: false, btnLoading: false,}})
        if (success) {
          message.success('更新成功',()=>{
            hashHistory.push({
              pathname: '/home/realEstateConsultantAdmin/consultantAdmin',
            });
          })
        }
      } catch (e) {
        console.log(e)
      }
    },
    // 获取单个置业顾问信息
    *getConsultantInfo({ payload }, { put, call, select }) {
      console.warn(payload, 'payload value');
      try {
        const { success, data } = yield call(getConsultantInfo, parse(payload))
        if (success) {
          console.log(data);
          let createConsultantParams = {
            name:data.name,
            idCardNo:data.idCardNo,
            productId:data.productId,
            oldProductId:data.productId,
            province:data.provinceCode,
            sex:data.sex,
            mobile:data.mobile,
            propertyConsultantId:data.counselorId,
            city:data.cityCode,
            outerAccount:data.outerAccount,
            displayAvgPoints:data.displayAvgPoints,
          };
          let isHasScore = data.displayAvgPoints ? true : false
            yield put({ type: 'updateStore', payload: { isHasScore } })
            console.log(data.displayAvgPoints)
            console.log(isHasScore)

            console.log(createConsultantParams)
          const { success, rows } = yield call(houseListGet, { province: data.provinceCode, city: data.cityCode, pageSize: 10000 });
          if (success) {
            yield put({ type: 'updateStore', payload: { projectList: rows } })
          }
          yield put({ type: 'updateStore', payload: { createConsultantParams,flagZeroId:data.productId }})
        }
      } catch (e) {
        console.log(e)
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
